home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / specfun / erfinv.m < prev    next >
Text File  |  1997-05-26  |  2KB  |  61 lines

  1. ## Copyright (C) 1995, 1996  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage:  erfinv (x)
  18. ##
  19. ## Computes the inverse of the error function erf.
  20.  
  21. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  22. ## Created: 27 September 1994
  23. ## Adapted-By: jwe
  24.  
  25. function y = erfinv (x)
  26.   
  27.   if (nargin != 1)
  28.     usage ("erfinv (x)");
  29.   endif
  30.   
  31.   [m, n] = size (x);  
  32.   x = reshape (x, m * n, 1);
  33.   y = zeros (m * n, 1);
  34.   
  35.   i = find ((x < -1) | (x > 1));
  36.   if any (i)
  37.     y(i) = NaN * ones (length (i), 1);
  38.   endif
  39.  
  40.   t = find (x == -1);
  41.   y (t) = (-Inf) * ones (size (t));
  42.  
  43.   t = find (x == 1);
  44.   y (t) = Inf * ones (size (t));
  45.  
  46.   i = find ((x > -1) & (x < 1));
  47.   if any (i)
  48.     s = sqrt (pi) / 2;
  49.     z_old = ones (length (i), 1);
  50.     z_new = zeros (length (i), 1);
  51.     while (any (any (abs (z_new - z_old) > 2 * eps)))
  52.       z_old = z_new;
  53.       z_new = z_old - (erf (z_old) - x(i)) .* exp (z_old.^2) * s;
  54.     endwhile
  55.     y(i) = z_new;
  56.   endif
  57.   
  58.   y = reshape (y, m, n);
  59.     
  60. endfunction
  61.